package com.roger.match.library.util;

import ohos.agp.components.Component;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

public class Utils {
    static int SCREEN_WIDTH_PIXELS;
    static int SCREEN_HEIGHT_PIXELS;
    static float SCREEN_DENSITY;
    static int SCREEN_WIDTH_DP;
    public static int SCREEN_HEIGHT_DP;
    private static boolean sInitialed;

    public static void init(Context context) {
        if (sInitialed || context == null) {
            return;
        }
        sInitialed = true;
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        SCREEN_WIDTH_PIXELS = attributes.width;
        SCREEN_HEIGHT_PIXELS = attributes.height;
        SCREEN_DENSITY = attributes.densityPixels;
        SCREEN_WIDTH_DP = (int) (SCREEN_WIDTH_PIXELS / attributes.densityPixels);
        SCREEN_HEIGHT_DP = (int) (SCREEN_HEIGHT_PIXELS / attributes.densityPixels);
    }

    /**
     * dp转px
     *
     * @param dp dp
     * @return dp2px
     */
    public static int dp2px(float dp) {
        final float scale = SCREEN_DENSITY;
        return (int) (dp * scale + 0.5f);
    }

    public static int designedDP2px(float designedDp) {
        if (SCREEN_WIDTH_DP != 320) {
            designedDp = designedDp * SCREEN_WIDTH_DP / 320f;
        }
        return dp2px(designedDp);
    }

    /**
     * 设置padding
     *
     * @param view 目标view
     * @param left left
     * @param top top
     * @param right right
     * @param bottom bottom
     */
    public static void setPadding(final Component view, float left, float top, float right, float bottom) {
        view.setPadding(designedDP2px(left), dp2px(top), designedDP2px(right), dp2px(bottom));
    }
}
